
import pickle
import os
import numpy as np
from global_data_2 import *

result_filedir = f'../result/{exp_key}'
# all_start_time = 1586318400
all_start_time = 1586145600
all_last_time = 1586318400


def get_score_or_matrix_data(number=2):
    filepath = f'{result_filedir}/result_for_period2_{number}'
    f_list = sorted([f for f in os.listdir(filepath) if 'test_score' in f])
    score_list, timestamp_list = [], []
    score_all_matrix = []
    for f in f_list:
        try:
            timestamp = int(f.split('-')[0])
            if (timestamp >= all_start_time) and (timestamp <= all_last_time):
                with open(os.path.join(filepath, f), 'rb') as fr:
                    ip_list, score, label = pickle.load(fr)
                    # score 簇内元素数量，19（指标数量）
                    score_list.append(np.nansum(score, axis=1))
                    score_all_matrix.append(score)
                    timestamp_list.append(timestamp)
        except:
            continue
    # score_matrix 时间长度，簇内元素数量
    score_matrix = np.array(score_list)
    score_all_matrix = np.array(score_all_matrix)
    return score_matrix, timestamp_list, ip_list, score_all_matrix


def save_file(number=2):
    os.makedirs(f'{exp_key}/label_data', exist_ok=True)
    score_matrix, timestamp_list, ip_list, score_all_matrix = get_score_or_matrix_data(number)
    with open(f'{exp_key}/label_data/{number}_score.pkl', 'wb') as fw:
        print(f"len(score_matrix):{len(score_matrix)}")
        pickle.dump([score_matrix, timestamp_list, ip_list], fw)
    np.save(f'{exp_key}/label_data/{number}_score_all.npy', score_all_matrix)


for _i in range(1, cluster_num+1):
    save_file(_i)

